/*

 Copyright (c) 2008-2009 Fabrizio Pistonesi

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

 */

#include "panel.h"

Panel::Panel(IrrlichtDevice *device, s32 idn, stringc background, position2d<s32> position, SColor color) : Node(device, idn, ENT_PANEL)
  {
    if (!Device->getFileSystem()->existFile(background.c_str()))
      {
        background = "missing.png";
      }
    else
      {
        Texture = Device->getVideoDriver()->getTexture(background.c_str());
      }
    s32 width = Texture->getSize().Width;
    s32 height = Texture->getSize().Height;
    if (position.X == -1) position.X = Device->getVideoDriver()->getScreenSize().Width / 2 - width / 2;
    if (position.Y == -1) position.Y = Device->getVideoDriver()->getScreenSize().Height / 2 - height / 2;
    IGUIEnvironment *gui = Device->getGUIEnvironment();
    PanelGUI = gui->addImage(Texture, position);
    PanelGUI->setColor(color);

    //serialize
    Background = background;
    Position = position;
    Color = color;
  }

Panel::~Panel()
  {
    PanelGUI->remove();
  }

void Panel::reload(stringc background)
  {
    if(!Device->getFileSystem()->existFile(background.c_str()))
    {
    	background = "missing.png";
    }
	Texture = Device->getVideoDriver()->getTexture(background.c_str());
    PanelGUI->setImage(Texture);

    //serialize
    Background = background;
  }

void Panel::move(position2d<s32> position)
  {
    s32 width = Texture->getSize().Width;
    s32 height = Texture->getSize().Height;
    rect<s32> rect(position.X, position.Y, position.X + width, position.Y + height);
    PanelGUI->setRelativePosition(rect);

    //serialize
    Position = position;

  }

rect<s32> Panel::getPosition()
  {
    return PanelGUI->getRelativePosition();
  }

void Panel::setColor(int a, int r, int g, int b)
  {
    PanelGUI->setColor(SColor(a,r,g,b));

    //serialize
    Color = SColor(a,r,g,b);
  }
